﻿var setupTask;

//设置每次副本开启的分钟段(分钟:为单位)
var startminute = 30;

//设置每次活动结束的时间(分钟:为单位)
var overtime = 20;

//设置多久开启一次(小时:为单位)
var starttime = 2;

function init() {
	scheduleNew();
}

function scheduleNew() {
	em.setProperty("state", "false");
	em.setProperty("endEvent", "true");
	em.setProperty("check", "0");
	em.setProperty("maxCheck", "9999999");
	em.setProperty("Message", "");
	em.setProperty("Win", "0");
	var cal = java.util.Calendar.getInstance();

	var hour = cal.get(java.util.Calendar.HOUR_OF_DAY); //获得小时 24小时制
	var minute = cal.get(java.util.Calendar.MINUTE); //获得分钟
	var second = cal.get(java.util.Calendar.SECOND); //获得秒
	cal.set(java.util.Calendar.HOUR, 0); //设定的小时 12小时制
	cal.set(java.util.Calendar.MINUTE, startminute); //设定的分钟
	cal.set(java.util.Calendar.SECOND, 0); //设定的秒钟
	var nextTime = cal.getTimeInMillis();
	while (nextTime <= java.lang.System.currentTimeMillis()) {
		nextTime += 1000 * 60 * 60 * starttime; //设置多久开启
	}
	setupTask = em.scheduleAtTimestamp("startEvent", nextTime);
}

function startEvent() {
	em.setProperty("state", "true");
	em.setProperty("endEvent", "false");
	em.setProperty("check", 0);
	em.setProperty("maxCheck", "" + getMaxCheck(Math.floor(Math.random() * 2)));
	em.setProperty("Message", "");
	em.setProperty("Win", "0");
	var cal = java.util.Calendar.getInstance();
	var hour = cal.get(java.util.Calendar.HOUR_OF_DAY); //获得小时  24小时制
	var minute = cal.get(java.util.Calendar.MINUTE); //获得分钟
	var second = cal.get(java.util.Calendar.SECOND); //获得秒
	cal.set(java.util.Calendar.HOUR, 0); //设定的小时 12小时制
	cal.set(java.util.Calendar.MINUTE, startminute); //设定的分钟
	cal.set(java.util.Calendar.SECOND, 0); //设定的秒钟
	var nextTime = cal.getTimeInMillis();
	while (nextTime <= java.lang.System.currentTimeMillis()) {
		nextTime += 1000 * 60 * overtime; //设置多久结束
	}

	setupTask = em.scheduleAtTimestamp("finishEvent", nextTime);
	em.broadcastServerMsg(5121027, "许愿活动已经开始，请通过万能NPCPPMS专属副本进入！您的愿望由我来实现！", true);
	em.setProperty("start", "1"); //等待状态
	em.broadcastServerMsg("[许愿活动]  活动已经开启。" + overtime + "分钟后结束");
}

function finishEvent() {
	if (em.getProperty("endEvent").equals("false")) {
		em.broadcastServerMsg("[许愿活动] 活动已经结束。每相隔" + starttime + "个小时。开启一次.本次许愿还有一次没有被领取到..请大家再接再厉。");
	} else {
		var text = "";
		text += "[许愿活动] 本次活动所有奖励已经全部发放!!! " + starttime + "个小时后会再次开启许愿之门";
		em.broadcastServerMsg(text);
	}
	scheduleNew();
}

function cancelSchedule() {
	if (setupTask != null) {
		setupTask.cancel(true);
	}
}

function getMaxCheck(type) {
	switch (type) {
	case 0:
		return 30;
	case 1:
		return 40;
	case 2:
		return 50;
	}
	return 60;
}

function rand(lbound, ubound) {
	return Math.floor(Math.random() * (ubound - lbound)) + lbound;
}
